<template>
  <div class="update-gender">
    <van-picker
      show-toolbar
      :columns="columns"
      :default-index="defaulteIndex"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onGenderChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'

export default {
  name: 'UpdateGender',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      defaulteIndex: this.value,
      localGender: this.value
    }
  },
  mounted () {

  },
  methods: {
    onGenderChange (picker, value, index) {
      this.defaulteIndex = index
    },
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidclick: true
      })
      await updateUserProfile({
        gender: this.defaulteIndex
      })
      this.$toast.success('保存成功')
      this.$emit('input', this.defaulteIndex)
      this.$emit('close')
      this.$toast.success('保存成功')
    }
  }
}
</script>

<style lang="less" scoped>

</style>
